home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Mac Parts / Files / Directories / CatInfo.cp < prev    next >
Text File  |  2000-06-23  |  6KB  |  263 lines

  1. // CatInfo.cp
  2.  
  3. #ifndef CatInfo_h
  4. #include "CatInfo.h"
  5. #endif
  6. #ifndef FileNotFoundError_h
  7. #include "FileNotFoundError.h"
  8. #endif
  9. #ifndef DirectoryNotFoundError_h
  10. #include "DirectoryNotFoundError.h"
  11. #endif
  12. #ifndef HardwareVolumeLockError_h
  13. #include "HardwareVolumeLockError.h"
  14. #endif
  15. #ifndef SoftwareVolumeLockError_h
  16. #include "SoftwareVolumeLockError.h"
  17. #endif
  18. #ifndef FileLockError_h
  19. #include "FileLockError.h"
  20. #endif
  21. #ifndef FilePermissionError_h
  22. #include "FilePermissionError.h"
  23. #endif
  24. #ifndef NotADirectoryError_h
  25. #include "NotADirectoryError.h"
  26. #endif
  27. #ifndef BadFileNameError_h
  28. #include "BadFileNameError.h"
  29. #endif
  30.  
  31. CatInfo::CatInfo()
  32.   {
  33.     hFileInfo.ioCompletion = 0;
  34.     hFileInfo.ioNamePtr = location.name;
  35.     hFileInfo.ioFVersNum = 0;
  36.   }
  37.  
  38. CatInfo::CatInfo( const FileLocation& target )
  39.   {
  40.     hFileInfo.ioCompletion = 0;
  41.     hFileInfo.ioNamePtr = location.name;
  42.     hFileInfo.ioFVersNum = 0;
  43.     Get( target );
  44.   }
  45.  
  46. CatInfo::CatInfo( ::Directory directory )
  47.   {
  48.     hFileInfo.ioCompletion = 0;
  49.     hFileInfo.ioNamePtr = location.name;
  50.     hFileInfo.ioFVersNum = 0;
  51.     Get( directory );
  52.   }
  53.  
  54. CatInfo::CatInfo( ::Directory directory, ConstPString name )
  55.   {
  56.     hFileInfo.ioCompletion = 0;
  57.     hFileInfo.ioNamePtr = location.name;
  58.     hFileInfo.ioFVersNum = 0;
  59.     Get( directory, name );
  60.   }
  61.  
  62. CatInfo::CatInfo( ::Directory directory, int16 index )
  63.   {
  64.     hFileInfo.ioCompletion = 0;
  65.     hFileInfo.ioNamePtr = location.name;
  66.     hFileInfo.ioFVersNum = 0;
  67.     Get( directory, index );
  68.   }
  69.  
  70. CatInfo::CatInfo( const CInfoPBRec& info )
  71.   {
  72.     Assert( info.hFileInfo.ioNamePtr != 0 );
  73.     
  74.     *static_cast<CInfoPBRec *>( this ) = info;
  75.     hFileInfo.ioCompletion = 0;
  76.     hFileInfo.ioNamePtr = location.name;
  77.     hFileInfo.ioFVersNum = 0;
  78.  
  79.     location.vRefNum = hFileInfo.ioVRefNum;
  80.     location.parID = hFileInfo.ioFlParID;
  81.     location.SetName( info.hFileInfo.ioNamePtr );
  82.   }
  83.  
  84. CatInfo::CatInfo( const CatInfo& source )
  85.   : CInfoPBRec( source ),
  86.      location( source.location )
  87.   {
  88.     hFileInfo.ioNamePtr = location.name;
  89.   }
  90.  
  91. void CatInfo::operator=( const CatInfo& source )
  92.   {
  93.     static_cast< CInfoPBRec& >( *this ) = source;
  94.     location = source.location;
  95.     hFileInfo.ioNamePtr = location.name;
  96.   }
  97.  
  98. void CatInfo::Get()
  99.   {
  100.     hFileInfo.ioVRefNum = location.Volume().RefNum();
  101.     hFileInfo.ioDirID = location.ParentID().Number();
  102.     hFileInfo.ioFDirIndex = 0;
  103.     ThrowError( PBGetCatInfoSync( this ) );
  104.     
  105.     Assert( hFileInfo.ioVRefNum == location.vRefNum );
  106.     Assert( hFileInfo.ioFlParID == location.parID );
  107.   }
  108.  
  109. void CatInfo::Get( const FileLocation& target )
  110.   {
  111.     location = target;
  112.     Get();
  113.   }
  114.  
  115. void CatInfo::Get( ::Directory directory )
  116.   {
  117.     hFileInfo.ioVRefNum = directory.Volume().RefNum();
  118.     hFileInfo.ioDirID = directory.ID().Number();
  119.     hFileInfo.ioFDirIndex = -1;
  120.     ThrowError( PBGetCatInfoSync( this ) );
  121.     
  122.     location.vRefNum = hFileInfo.ioVRefNum;
  123.     location.parID = hFileInfo.ioFlParID;
  124.   }
  125.  
  126. void CatInfo::Get( ::Directory directory, ConstPString name )
  127.   {
  128.     location.Set( directory, name );
  129.     Get();
  130.   }
  131.  
  132. void CatInfo::Get( ::Directory directory, int16 index )
  133.   {
  134.     Assert( index > 0 );
  135.     hFileInfo.ioVRefNum = directory.Volume().RefNum();
  136.     hFileInfo.ioDirID = directory.ID().Number();
  137.     hFileInfo.ioFDirIndex = index;
  138.     ThrowError( PBGetCatInfoSync( this ) );
  139.  
  140.     location.vRefNum = hFileInfo.ioVRefNum;
  141.     location.parID = hFileInfo.ioFlParID;
  142.     Assert( directory.Volume().RefNum() == location.vRefNum );
  143.     Assert( directory.ID().Number() == location.parID );
  144.   }
  145.  
  146. bool CatInfo::TryToGet( ::Directory directory, int16 index )
  147.   {
  148.     Assert( index > 0 );
  149.     hFileInfo.ioVRefNum = directory.Volume().RefNum();
  150.     hFileInfo.ioDirID = directory.ID().Number();
  151.     hFileInfo.ioFDirIndex = index;
  152.     
  153.     OSErr error = PBGetCatInfoSync( this );
  154.     
  155.     if ( error == fnfErr )
  156.         return false;
  157.     
  158.     ThrowError( error );
  159.     
  160.     location.vRefNum = hFileInfo.ioVRefNum;
  161.     location.parID = hFileInfo.ioFlParID;
  162.     Assert( directory.Volume().RefNum() == location.vRefNum );
  163.     Assert( directory.ID().Number() == location.parID );
  164.     
  165.     return true;
  166.   }
  167.  
  168. bool CatInfo::TryToGet()
  169.   {
  170.     hFileInfo.ioVRefNum = location.Volume().RefNum();
  171.     hFileInfo.ioDirID = location.ParentID().Number();
  172.     hFileInfo.ioFDirIndex = 0;
  173.  
  174.     OSErr error = PBGetCatInfoSync( this );
  175.  
  176.     if ( error == fnfErr )
  177.         return false;
  178.  
  179.     ThrowError( error );
  180.     
  181.     return true;
  182.   }
  183.  
  184. bool CatInfo::TryToGet( const FileLocation& target )
  185.   {
  186.     location = target;
  187.     return TryToGet();
  188.   }
  189.  
  190. void CatInfo::Set()
  191.   {
  192.     hFileInfo.ioVRefNum = location.Volume().RefNum();
  193.     hFileInfo.ioDirID = location.ParentID().Number();
  194.     ThrowError( PBSetCatInfoSync( this ) );
  195.   }
  196.  
  197. void CatInfo::Set( const FileLocation& target )
  198.   {
  199.     location = target;
  200.     Set();
  201.   }
  202.  
  203. ::Directory CatInfo::AsDirectory() const
  204.   {
  205.     if ( !IsDirectory() )
  206.         throw NotADirectoryError( noErr );
  207.     
  208.     return ::Directory( location.Volume(), Directory().ID() );
  209.   }
  210.  
  211. void CatInfo::Up()
  212.   {
  213.     Assert( !IsRoot() );
  214.     if ( IsRoot() )
  215.         ThrowError( dirNFErr );
  216.  
  217.     Get( location.Parent() );
  218.   }
  219.  
  220. void CatInfo::Down( ConstPString child )
  221.   {
  222.     Assert( IsDirectory() );
  223.     if ( !IsDirectory() )
  224.         throw NotADirectoryError( noErr );
  225.  
  226.     location.SetParentID( Directory().ID() );
  227.     location.SetName( child );
  228.     Get();
  229.   }
  230.  
  231. void CatInfo::Sideways( ConstPString sibling )
  232.   {
  233.     location.SetName( sibling );
  234.     Get();
  235.   }
  236.  
  237. void CatInfo::SetLabel( uint8 label )
  238.   {
  239.     Assert( label < 8 );
  240.     hFileInfo.ioFlFndrInfo.fdFlags &= uint16( ~kColor );
  241.     hFileInfo.ioFlFndrInfo.fdFlags != label << 1;
  242.   }
  243.  
  244. void CatInfo::ThrowError( OSErr error )
  245.   {
  246.     if ( error == noErr )
  247.         return;
  248.     
  249.     switch ( error )
  250.       {
  251.         case fnfErr:                throw FileNotFoundError( error );
  252.         case dirNFErr:                throw DirectoryNotFoundError( error );
  253.         case wPrErr:                throw HardwareVolumeLockError( error );
  254.         case vLckdErr:                throw SoftwareVolumeLockError( error );
  255.         case fLckdErr:                throw FileLockError( error );
  256.         case afpAccessDenied:    throw FilePermissionError( error );
  257.         case bdNamErr:                throw BadFileNameError( error );
  258.       }
  259.     
  260.     Assert( ("Unexpected error", 0) );
  261.     throw FileError( error );
  262.   }
  263.